package com.webcash.CNBoard.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.webcash.CNBoard.Model.MyProject;
import com.webcash.CNBoard.Model.Work;
import com.webcash.CNBoard.Model.WorkSearch;
import com.webcash.CNBoard.Service.ProjectService;

@Controller
@RequestMapping("/project/workList")
@SessionAttributes({"login", "myProject"} )
public class ProjectWorkListController {
	@Autowired
	ProjectService projectService;
	@Autowired
	MyProject myProject;
	@RequestMapping
	public String workList(@RequestParam(required=false) String num, Model model){
		
		int pageNum = 1;
		int count = projectService.getWorkListCountByProjectNo(myProject.getProjectNo());

		if(num != null)
			pageNum = Integer.parseInt(num);
		
		
		List<Work> workList = projectService.getWorkListByProjectNo(myProject.getProjectNo(), pageNum);
		
		
		model.addAttribute("workCount", count);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("workList", workList);
		
		return "project/workList";
	}
	
	@RequestMapping("/search")
	public String workListSearch(@ModelAttribute WorkSearch search, @RequestParam(required=false) String num, Model model){
		int pageNum = 1;
		int count = projectService.getWorkListCountByProjectNoAndWorkSearch(myProject.getProjectNo(), search);
		
		if(num != null)
			pageNum = Integer.parseInt(num);
		
		List<Work> workList = projectService.getWorkListByProjectNoAndWorkSearch(myProject.getProjectNo(), search, pageNum);
		
		model.addAttribute("workCount", count);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("workList", workList);
		model.addAttribute("search", search);
		
		return "project/workList";
	}
	
}
